<?php

namespace App\Http\Controllers\Api\V1;

use App\Models\Course;
use App\Models\CourseUser;
use App\Models\CoursePayLog;
use App\Models\CourseStudyLog;
use Illuminate\Http\Request;
use GuzzleHttp\Client;
use Illuminate\Support\Arr;

class BasePayController extends BaseController
{
   protected $wxPayConfig;
   protected $aliPayConfig;
   
   public function __construct() {
	   $this->wxPayConfig = [
	   	   'appid' => 'wxb3fxxxxxxxxxxx', // APP APPID
	   	   'app_id' => 'wxb3fxxxxxxxxxxx', // 公众号 APPID
	   	   'miniapp_id' => 'wxb3fxxxxxxxxxxx', // 小程序 APPID
	   	   'mch_id' => '1508613241',
	   	   'key' => '597b1847e761db4b97cb3035fc6e95e2',
	   	   'notify_url' => 'http://'.env('API_DOMAIN').'/pay/wx_notify',
	   	   'cert_client' => './cert/apiclient_cert.pem', // optional，退款等情况时用到
	   	   'cert_key' => './cert/apiclient_key.pem',// optional，退款等情况时用到
	   	   'log' => [ // optional
	   		   'file' => storage_path('logs').'/wechat.log',
	   		   'level' => 'debug', // 建议生产环境等级调整为 info，开发环境为 debug
	   		   'type' => 'single', // optional, 可选 daily.
	   		   'max_file' => 30, // optional, 当 type 为 daily 时有效，默认 30 天
	   	   ],
	   	   'http' => [ // optional
	   		   'timeout' => 5.0,
	   		   'connect_timeout' => 5.0,
	   		   // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
	   	   ],
	   	   'mode' => 'dev', // optional, dev/hk;当为 `hk` 时，为香港 gateway。
	   ];
	   
	   $this->aliPayConfig = [
		   'app_id' => '2017123101416151',
		   'notify_url' => 'http://'.env('API_DOMAIN').'/pay/ali_notify',
		   'return_url' => 'http://'.env('API_DOMAIN').'/pay/ali_return',
		   'ali_public_key' => 'MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAv6/TCk6dR2LAMixHeaXDCDYGn5YY7JnkPZu3SFg1jQH8fmOV4JkvTuSM9JUIUn/16rUWPeEVqx6BQ+b/FF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bZrr8xcci8Z8oi4v+D6/0IuXRKt6+gPvk5zmraaZT7rFSiLOsUMRygqyC/vC59GR1rkuahiXlwFUquvxyoNWpSKpCFmJjmvibeqsvj6arYNmdN7d3otZZvU3ND9uHAL9iw3f9gqt4uvyCAagJMCgYEA2V0nch/YnJaWn9R+eRDhSrkZ9FRodWPfUjAvYIG1wWB10zz7fTXZu0J742UQnb1EnlsUh+jTlGqYvMjh60MnqEilOv0SSNCO56vaptl78m3UFpuZKv9tqbbGArt69hoYlY7X01pRbqsFag/8/c9lj+KEmoVqcn2ytWGsFMbTVisCgYEAidP8RXa1/rDHo+LzDHBz1ZXaZ01mw0ZF7mQ5OCwmdubY9jwmaCY+Tnc4HVxyEbOH4WYfSaSeE/w3Ob4ZGGzDSVotIuUoHPzjSW5uxYER6MsQqgtj9T56yb2QIMkkCu4cnEeXWCGYEGJkEtOnikSBkje95cYq//8uXS6H2+tVoscCgYBuqDHws62+6hr8e113nNRVCi7gaChVuuZyHP3Bj8dMDyNsytrtP4D9CDex6D2xUC2iwJOPLUSphisD0PDSqHmD/pe/lB6+2w4leyPxVT74iFk0uToc3bSidd4Qfgl3SInDedj+TDiwsngrKsCNJgx6V8NSNyGd78gO9w539VCZyQKBgQCMmY/7fy5bJd1mvmcQ3rEDcYTku+6Fnm9xpPHj2D3UqZchsCEUJtAAWQxQqPPmquHNyS4E8g1DAIDlv3vMkkUx7zvKzJWupEjVEjvyflLnyxK4v9wUpyvBuroHYanJQM19Fe1idn4sakCnlAF/puv2B/KkmDLPKZr2BR9HrnOYmQ==',
		   'log' => [ // optional
			   'file' => storage_path('logs').'/alipay.log',
			   'level' => 'info', // 建议生产环境等级调整为 info，开发环境为 debug
			   'type' => 'single', // optional, 可选 daily.
			   'max_file' => 30, // optional, 当 type 为 daily 时有效，默认 30 天
		   ],
		   'http' => [ // optional
			   'timeout' => 5.0,
			   'connect_timeout' => 5.0,
			   // 更多配置项请参考 [Guzzle](https://guzzle-cn.readthedocs.io/zh_CN/latest/request-options.html)
		   ],
		   'mode' => 'dev', // optional,设置此参数，将进入沙箱模式
	   ];
	   parent::__construct();
   }

	/**
	 * 添加支付记录
	 *
	 */
	protected function query_pay_log($regId, $payType){
		$records = CoursePayLog::where('reg_id', $regId)->where('pay_type', $payType)->where('status', 0)->where('third_pay_id', '<>', '')->get();
		if(!$records){
			return [];
		}
		foreach ($records as $log) {
			$leSec = time()-strtotime($log->created_at);
			if($leSec > $log->expire){
				continue;
			}
		    $moreArr = json_decode($log->more, true);
			if($payType == 'xorpay_alipay'){
				if($moreArr && isset($moreArr['qr']) && $moreArr['qr']){
					return [
						'qr' => $moreArr['qr'],
						'pay_type' => $payType,
						'ali_appid' => env('ALIPAY_APPID'),
					];
				}
			}else if($payType == 'xorpay_native'){
				return [
					'qr' => $moreArr['qr'],
					'pay_type' => $payType,
					'ali_appid' => env('ALIPAY_APPID'),
				];
			}
		}
		return [];
	}

	/**
	 * 添加支付记录
	 *
	 */
	protected function add_pay_log($regId, $payType){
		$attributes = [
			'reg_id' => $regId,
			'pay_type' => $payType,
		];
		$record = CoursePayLog::create($attributes);
		if(!$record){
			return false;
		}
		$data = $record->toArray();
		return $data['id'];
	}
	
	/**
	 * 更新支付记录
	 *
	 */
	protected function update_pay_log($payId, $status, $thirdId = '', $detail = '', $expire = 0, $more = []){
		$upInfo = [
			'status' => $status,
		];
		if($thirdId){
			$upInfo['third_pay_id'] = $thirdId;
		}
		if($detail && is_array($detail)){
			$upInfo['detail'] = json_encode($detail);
		}
		if($expire){
			$upInfo['expire'] = $expire;
		}
		if($more && is_array($more)){
			$upInfo['more'] = json_encode($more);
		}
		if($upInfo){
			$upRet = CoursePayLog::where('id', $payId)->update($upInfo);
			return $upRet;
		}
		
		return false;
	}
	
}
